import router from '@/router/index'

export default {
  namespaced: true,
  state: {
    tabsList: [{ path: '/dashboard', title: '工作台' }],
    initList: [{ path: '/dashboard', title: '工作台' }]
  },
  getters: {},
  mutations: {
    // 增加
    setTabList(state, data) {
      let { path } = data
      let { title } = data
      if (!title) return
      if (title.constructor === Object) {
        title = path.includes('null') ? title.null : title.else
      }

      path = path === '/' ? '/dashboard' : path
      const flag = state.tabsList.some(item => item.path === path)
      if (!flag) {
        state.tabsList.push({ path, title })
      }
    },
    // 删除全部
    delAllTabList(state) {
      state.tabsList = [{ path: '/dashboard', title: '工作台' }]
    },
    // 单个 删除
    delTabList(state, path) {
      const index = state.tabsList.findIndex(item => item.path === path)
      state.tabsList.splice(index, 1)
      router.push(state.tabsList[index - 1].path)
    },
    // 关闭左侧
    delTabLeft(state, path) {
      if (path === '/dashboard') { return }
      const index = state.tabsList.findIndex(item => item.path === path)
      state.tabsList = [...state.initList, ...state.tabsList.slice(index)]
    },
    // 关闭右侧
    delTabRight(state, path) {
      const index = state.tabsList.findIndex(item => item.path === path)
      state.tabsList = [...state.tabsList.slice(0, index + 1)]
    },
    // 关闭其他
    delTabElse(state, path) {
      if (path === '/dashboard') {
        state.tabsList = [...state.initList]
        router.push('/')
        return
      }
      const index = state.tabsList.findIndex(item => item.path === path)
      state.tabsList = [...state.initList, ...state.tabsList.slice(index, index + 1)]
    },
    // 关闭全部
    delTabAll(state) {
      state.tabsList = [...state.initList]
      router.push('/')
    }
  }
}
